<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>打牌</title>
	<link rel="stylesheet" href="/game/bootstrap/css/bootstrap.min.css">
	<link rel="stylesheet" href="/game/js/mobile/need/layer.css">
	<link rel="stylesheet" href="/game/css/21room.css">
	<script src="/game/js/hallimg.js"></script>
	<script src="/game/js/pokers.js"></script>
</head>
<body>
	<div id="poker-main">
		<div class="poker-panel">
			<div class="col-md-9" id="poker-left-panel">
				<div class="col-md-12 radio">
				</div>
				<div class="col-md-12 lmr-panel">
					<div class="enemy-user-left">
						<ul class="pull-right" id="left_pokerlist">
							<li><img class="enemypokers first-poker" src="/game/img/pokerback.png"></li>
						</ul>
						<div class="user-enemy-a">
							<table>
								<tr><td><img class="enemy-head enemy-head-left" alt="asd"></td></tr>
								<tr><td id="enemy_nick_left">asd</td></tr>
								<tr><td><br></td></tr>
								<tr id="left-clock"><td><img src="/game/img/clock.png" class="clock"></td></tr>
								<tr><td><span id="left-time" class="enemy-count-time">  </span></td></tr>
							</table>
						</div>
					</div>
					<div class="middle-panel">
						<div class="col-md-12 round-pokers">
							<span id="end-user1"></span>
							<ul class="end-user1"></ul>
						</div>
						<div class="col-md-12 round-pokers">
							<span id="end-user2"></span>
							<ul class="end-user2"></ul>
						</div>
					</div>
					<div class="enemy-user-right">
						<div class="user-enemy-b">
							<table>
								<tr><td><img class="enemy-head enemy-head-right" alt="asd"></td></tr>
								<tr><td id="enemy_nick_right">ff</td></tr>
								<tr><td><br></td></tr>
								<tr id="right-clock"><td><img src="/game/img/clock.png" class="clock"></td></tr>
								<tr><td><span id="right-time" class="enemy-count-time"> </span></td></tr>
							</table>
						</div>
						<ul id="right_pokerlist">
							<li><img class="enemypokers first-poker" src="./img/pokerback.png"></li>
						</ul>
					</div>
				</div>

				<div class="col-md-12 bottom-user">
					<div class="col-md-12 btn-play">
						<img src="/game/img/clock.png" class="clock"> <span id="my-time">20</span>
						<img src="/game/img/btn_chou.png" id="btn-go" class="btn-go">
						<img src="/game/img/btn_kou.png" id="btn-pass" class="btn-go">
					</div>
					<div class="col-md-12 btn-ready">
						<img src="/game/img/btn_onready.png" id="btn-ready" class="btn-go">
						<img src="/game/img/btn_exit.png" id="btn-exit" class="btn-go">
					</div>
					<div class="col-md-12 my-pokers-panel">
						<ul id="my-pokers-panel">
							<li><img class="mypokers first-poker" src="/game/img/a.png"  value="aa"></li>
						</ul>
					</div>
				</div>
			</div>
			<div class="col-md-3" id="poker-right-panel">
				<div class="poker-me">
					<img src="/game/img/chesspanelright.png"> <br>
					<span>{{ $user_nick }}</span>
				</div>
				<div class="user-enemy">
					<table class="table table-default enemysinfos"></table>
				</div>
				<div class="user-chat">
					<!-- 聊天信息 -->
				</div>
				<div class="form-group">
					<input type="text" id="roomchatword" class="form-control">
					<button id="roomsendmsg" class="btn btn-success pull-right">发送</button>
				</div>
			</div>
		</div>
	</div>
</body>
	<script src="/game/js/jquery-1.11.1.min.js"></script>
	<script src="/game/js/layer.js"></script>
	<script src="/game/bootstrap/js/bootstrap.min.js"></script>
	<script>
		var user_nick = window.parent.user_nick;
		var isgaming = 'false';
		//存放手中牌数组
		var mypokers = [];

		var enemy_left = '';
		var enemy_right = '';

		//刷新房间
		function freshorder(info) {
			enemy_left = '';
			enemy_right = '';
			//找出对手的位置
			for (var i = 0; i < info[0].length; i++) {
				if (info[0][i].nick == user_nick) {
					if (i == 0) {
						if (typeof(info[0][i+1]) != 'undefined') {
							enemy_left = info[0][i+1].nick;
						}
						if (typeof(info[0][i+2]) != 'undefined') {
							enemy_right = info[0][i+2].nick;
						}
					} else if (i == 1) {
						if (typeof(info[0][i-1]) != 'undefined') {
							enemy_right = info[0][i-1].nick;
						}
						if (typeof(info[0][i+1]) != 'undefined') {
							enemy_left = info[0][i+1].nick;
						}
					} else if (i == 2) {
						if (typeof(info[0][i-2]) != 'undefined') {
							enemy_left = info[0][i-2].nick;
						}
						if (typeof(info[0][i-1]) != 'undefined'){
							enemy_right = info[0][i-1].nick;
						}
					}
					break;
				}
			}

			if (info[1].user_b_nick == enemy_left) {
				$('#enemy_nick_left').html(enemy_left);
				$('.enemy-head-left').attr('src','./img/chesspanelright.png');
			} else if (info[1].user_a_nick == enemy_left) {
				$('#enemy_nick_left').html(enemy_left);
				$('.enemy-head-left').attr('src','./img/chesspanelright.png');
			} else if (info[1].user_c_nick == enemy_left) {
				$('#enemy_nick_left').html(enemy_left);
				$('.enemy-head-left').attr('src','./img/chesspanelright.png');
			} else {
				enemy_left = '';
				$('#enemy_nick_left').html('未加入');
				$('.enemy-head-left').attr('src',nobody_png);
			}

			if (info[1].user_b_nick == enemy_right) {
				$('#enemy_nick_right').html(enemy_right);
				$('.enemy-head-right').attr('src','./img/chesspanelright.png');
			} else if (info[1].user_a_nick == enemy_right) {
				$('#enemy_nick_right').html(enemy_right);
				$('.enemy-head-right').attr('src','./img/chesspanelright.png');
			} else if (info[1].user_c_nick == enemy_right) {
				$('#enemy_nick_right').html(enemy_right);
				$('.enemy-head-right').attr('src','./img/chesspanelright.png');
			} else {
				enemy_right = '';
				$('#enemy_nick_right').html('未加入');
				$('.enemy-head-right').attr('src',nobody_png);
			}

			//追加玩家信息到状态列表
			$('.enemysinfos').html('');
			if (info[1].user_a_nick != 'off') {
				$('.enemysinfos').append('<tr><td>'+info[1].user_a_nick+'</td><td>'+info[1].user_a_point+'%</td><td>'+info[1].user_a_status+'</td></tr>');
			}
			if (info[1].user_b_nick != 'off') {
				$('.enemysinfos').append('<tr><td>'+info[1].user_b_nick+'</td><td>'+info[1].user_b_point+'%</td><td>'+info[1].user_b_status+'</td></tr>');
			}
			if (info[1].user_c_nick != 'off') {
				$('.enemysinfos').append('<tr><td>'+info[1].user_c_nick+'</td><td>'+info[1].user_c_point+'%</td><td>'+info[1].user_c_status+'</td></tr>');
			}

		}

		//追加聊天到列表
		function roommsgadd(info) {
			var temp = JSON.parse(info);
			$('.user-chat').append('<div class="chat-list"><b>'+temp.nick+' 说：</b><p>'+temp.msg+'</p></div>');
			$('.user-chat').scrollTop($('.user-chat')[0].scrollHeight);
		}

		//初始化对局
		function gameinit(info) {
			$('.btn-ready').hide();
			window.parent.letsplay();
			isgaming = true;
		}

		//让用户选择要不要牌
		function yaobuyao() {
			$('.btn-play').children().show();

			$('#left-clock').hide();
			$('#left-time').hide();
			$('#right-clock').hide();
			$('#right-time').hide();

			$('#my-time').html('20');
			if (typeof(timea) != 'undefined') { clearInterval(timea); }
			timea = setInterval(mydaojishi,1000);
		}

		//我的倒计时
		function mydaojishi(){
			var t = $('#my-time').html();
			t = t - 1;
		    $('#my-time').html(t);
		    if(t==0){
		    	$('.btn-play').children().hide();
		    	window.parent.chouyizhang();
		        clearInterval(timea);
		    }
		}

		//等待对手动作
		function waiting(nick) {
			if (enemy_left == nick) {
				if (typeof(timeb) != 'undefined') { clearInterval(timeb); }
				$('#right-clock').hide();
				$('#right-time').hide();
				$('#left-clock').show();
				$('#left-time').show();
				$('#left-time').html('20');
				timeb = setInterval(leftdaojishi,1000);
			} else if (enemy_right == nick) {
				if (typeof(timec) != 'undefined') { clearInterval(timec); }
				$('#left-clock').hide();
				$('#left-time').hide();
				$('#right-clock').show();
				$('#right-time').show();
				$('#right-time').html('20');
				timec = setInterval(rightdaojishi,1000);
			}
		}

		//左边倒计时
		function leftdaojishi() {
			var t = $('#left-time').html();
			t = t - 1;
		    $('#left-time').html(t);
		    if(t==0){
		    	$('#left-clock').hide();
				$('#left-time').hide();
		        clearInterval(timeb);
		    }
		}

		//右边倒计时
		function rightdaojishi() {
			var t = $('#right-time').html();
			t = t - 1;
		    $('#right-time').html(t);
		    if(t==0){
		    	$('#right-clock').hide();
				$('#right-time').hide();
		        clearInterval(timec);
		    }
		}

		//有人抓了一张牌
		function someonegotpoker(info) {
			if (info.nick == enemy_left) {
				$('#left_pokerlist').append('<li><li><img class="enemypokers" src="/game/img/pokerback.png"></li>');
			} else if(info.nick == enemy_right) {
				$('#right_pokerlist').append('<li><img class="enemypokers" src="/game/img/pokerback.png"></li>');
			}
			waiting(info.next);
		}

		//我拿到了一张牌
		function igotpoker(info) {
			$('#my-pokers-panel').append('<li><img class="mypokers" src="'+eval(info.poker)+'" value="'+info.value+'"></li>');
		}

		//有人输了
		function oneloose(info) {
			if (enemy_left == info.nick) {
				$('#left_pokerlist').append('<li><li><img class="enemypokers" src="/game/img/burst.png"></li>');
			} else if (enemy_right == info.nick) {
				$('#right_pokerlist').append('<li><img class="enemypokers" src="/game/img/burst.png"></li>');
			} else if (info.nick == user_nick) {
				$('#my-pokers-panel').append('<li><img class="mypokers" src="/game/img/iburst.png"></li>');
			}
		}

		//本人放弃抓牌
		$('#btn-pass').click(function(){
			clearInterval(timea);
			$('.btn-play').children().hide();
	    	window.parent.wobuyao();
		});

		//本人要抓牌
		$('#btn-go').click(function(){
			clearInterval(timea);
			$('.btn-play').children().hide();
			window.parent.chouyizhang();
		});

		//游戏结束
		function gameover(info) {
			if (typeof(timec) != 'undefined') {
				clearInterval(timec)
			}
			if (typeof(timea) != 'undefined') {
				clearInterval(timea);
			}
			if (typeof(timeb) != 'undefined') {
				clearInterval(timeb);
			}

			isgaming = false;
			//隐藏所有工具
			allhide();
			var endstatus = '和局';
			var enemyleft = '';
			var enemyright = '';
			var enemyleftpokers = [];
			var enemyrightpokers = [];
			var winner = [];
			var loser = [];
			var ranout = '';

			if (typeof(info.ranout) != 'undefined') {
				loser = info.ranout;
				showendselect(winner, loser, endstatus, info.ranout);
				return;
			}

			//找出所有的LOSER
			for (var i = info.lose.length - 1; i >= 0; i--) {

				if (typeof(info.lose[i][0]) != 'undefined' && info.lose[i][0] == user_nick) {
					loser = loser + '&nbsp;' + user_nick;
					endstatus = '失败';
				}

				if (typeof(info.lose[i][0]) != 'undefined' && info.lose[i][0] == enemy_left) {
					enemyleft = info.lose[i][0];
					loser = enemyleft + '&nbsp;' + loser;
					for (var k = info.lose[i][1].length - 1; k >= 0; k--) {
						enemyleftpokers.push(info.lose[i][1][k]);
					}
				}

				if (typeof(info.lose[i][0]) != 'undefined' && info.lose[i][0] == enemy_right) {
					enemyright = info.lose[i][0];
					loser = enemyright + '&nbsp;' + loser;
					for (var t = info.lose[i][1].length - 1; t >= 0; t--) {
						enemyrightpokers.push(info.lose[i][1][t]);
					}
				}
			}

			//找出所有的Winner
			for (var i = info.win.length - 1; i >= 0; i--) {

				if (typeof(info.win[i][0]) != 'undefined' && info.win[i][0] == user_nick) {
					winner = winner + '&nbsp;' + user_nick;
					endstatus = '胜利';
				}

				if (typeof(info.win[i][0]) != 'undefined' && info.win[i][0] == enemy_left) {
					enemyleft = info.win[i][0];
					winner = winner + '&nbsp;' + enemyleft;
					for (var k = info.win[i][1].length - 1; k >= 0; k--) {
						enemyleftpokers.push(info.win[i][1][k]);
					}
				}

				if (typeof(info.win[i][0]) != 'undefined' && info.win[i][0] == enemy_right) {
					enemyright = info.win[i][0]
					winner = winner + '&nbsp;' + enemyright;
					for (var t = info.win[i][1].length - 1; t >= 0; t--) {
						enemyrightpokers.push(info.win[i][1][t]);
					}
				}
			}

			$('#end-user1').html(enemyleft);
			$('#end-user2').html(enemyright);

			for (var i = enemyleftpokers.length - 1; i >= 0; i--) {
				$('.end-user1').append('<img class="middel_poker" src="'+eval(enemyleftpokers[i])+'">');
			}
			for (var i = enemyrightpokers.length - 1; i >= 0; i--) {
				$('.end-user2').append('<img class="middel_poker" src="'+eval(enemyrightpokers[i])+'">');
			}

			showendselect(winner, loser, endstatus, ranout);
		}

		//弹出对局结果
		function showendselect(winner,loser,endstatus,ranout) {
			var thecolor = "white";
			var themsg = '<div style="padding: 50px; font-family: "黑体"; line-height: 22px; color: #fff; font-weight: 300;"><p style="text-align: center;font-size: 40px;color: '+thecolor+';font-weight:bold;">'+endstatus+'</p><br><br><p style="text-align: center;font-size: 20px;">Win：'+winner+'</p><br><p style="text-align: center;font-size: 20px;">Lose：'+loser+'</p></div>';

			if (endstatus == '胜利') {
				thecolor = 'yellowgreen';
			} else if (endstatus == '失败') {
				thecolor = '#FD0005';
			} else {
				thecolor = 'white';
				themsg = '<div style="padding: 50px; font-family: "黑体"; line-height: 22px; color: #fff; font-weight: 300;"><p style="text-align: center;font-size: 40px;color: '+thecolor+';font-weight:bold;">'+endstatus+'</p><br><br><p style="text-align: center;font-size: 20px;">打平了!</p></div>';
				if (ranout != "") {
					themsg = '<div style="padding: 50px; font-family: "黑体"; line-height: 22px; color: #fff; font-weight: 300;"><p style="text-align: center;font-size: 40px;color: '+thecolor+';font-weight:bold;">'+endstatus+'</p><br><br><p style="text-align: center;font-size: 20px;">'+ranout+'逃跑了!</p></div>';
				}
			}

			layer.msg(themsg, {
				btn: ['再来一局', '退出房间'],
				shade: 0.3,
				closeBtn: false,
				area: '400px;',
				time: 0,
				resize: false,
				btnAlign: 'c',
				type: 1,
				moveType: 1,
				success: function(layero) {
				    var btn = layero.find('.layui-layer-btn');
				    btn.find('.layui-layer-btn0').click(function(){
				     	window.location.reload();
				    });
				    btn.find('.layui-layer-btn1').click(function(){

				    	window.parent.backhall();
				    });
				}
			});
		}

		//隐藏对战元素
		function allhide() {
			$('.btn-play').children().hide();
			$('#left-clock').hide();
			$('#left-time').hide();
			$('#right-clock').hide();
			$('#right-time').hide();
		}

		$(document).ready(function(){
			//隐藏所有对战元素
			allhide();
			//初始化房间信息
			window.parent.getroominfo();
			//绑定发送聊天事件
			$('#roomsendmsg').click(function(){
				if ($('#roomchatword').val() != '') {
					window.parent.sendroomchat($('#roomchatword').val());
					$('#roomchatword').val('');
				}
			});
			//绑定回车键发送事件
			$('#roomchatword').keyup(function(){
				var event = arguments.callee.caller.arguments[0]||window.event;
				if (event.keyCode ==13) {
					$('#roomsendmsg').click();
				}
			});
		});

		//退出房间
		$('#btn-exit').click(function(){
			window.parent.backhall();
		});

		//准备和取消准备
		$('#btn-ready').click(function(){
			if (this.value != 'ok') {
				this.value = 'ok';
				this.src = './img/btn_readycancel.png';
				window.parent.mystatus = '准备';
				window.parent.pushreadystatus('pushreadystatus'+'ok');
				$('#btn-exit').hide();
			} else {
				this.value = 'no';
				this.src = 'game/img/btn_onready.png';
				window.parent.mystatus = '等待';
				window.parent.pushreadystatus('pushreadystatus'+'no');
				$('#btn-exit').show();
			}
		});

	</script>

</html>
